package netctoss.demo.controller;

import netctoss.demo.entrty.Cost;
import netctoss.demo.entrty.Result;
import netctoss.demo.service.CostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("fee")
public class FeeController {

    @Autowired
    CostService costService;

    @RequestMapping("")
    public ModelAndView feeList(){
        return new ModelAndView("/fee/fee_list.html");
    }

    @RequestMapping("detail")
    public ModelAndView showDetail(){
        return new ModelAndView("/fee/fee_detail.html");
    }

    @RequestMapping("insertCost")
    public ModelAndView insertCost(){
        return new ModelAndView("/fee/fee_add.html");
    }

    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("/fee/fee_modi.html");
    }

    @RequestMapping("selectPage/{pageNum}/{pageSize}")
    public Result selectPage(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize, @RequestBody Map map){

        return new Result(true,"查询成功",costService.selectPage(pageNum,pageSize,map));
    }

    @RequestMapping("deleteOne")
    public Result deleteOne(@RequestBody Cost fee){
        fee.setCostStatus("2");
        return new Result(true,"删除成功",costService.updateByPrimaryKeySelective(fee));
    }

    @RequestMapping("doUpdate")
    public Result doUpdate(@RequestBody Cost fee){
        return new Result(true,"更新成功",costService.updateByPrimaryKeySelective(fee));
    }

    @RequestMapping("checkCostName")
    public int checkCostName(String cname){
        String str="[0-9a-zA-Z_\\u4e00-\\u9fa5]{1,50}";
        Pattern compile = Pattern.compile(str);
        Matcher matcher = compile.matcher(cname);
        if (matcher.matches()){
            return 1;
        }
        return 0;
    }

    @RequestMapping("checkDuration")
    public int checkDuration(String duration){
        String str1="[1-9][0-9]?";
        String str2="[1-5][0-9]{2}";
        String str3="[6][0]{2}";
        Pattern compile = Pattern.compile(str1);
        Matcher matcher = compile.matcher(duration);
        if (!matcher.matches()){
            compile = Pattern.compile(str2);
            matcher = compile.matcher(duration);
            if (!matcher.matches()){
                compile = Pattern.compile(str3);
                matcher = compile.matcher(duration);
                if (!matcher.matches()){
                    return 0;
                }
            }
        }
        return 1;
    }

    @RequestMapping("checkBaseCost")
    public int checkBaseCost(String baseCost){
        String str1="0.[0-9]{1,2}";
        String str2="0";
        String str3="[1-9][0-9]{0,4}";
        String str4="[1-9][0-9]{0,4}.[0-9]{1,2}";
        Pattern compile = Pattern.compile(str1);
        Matcher matcher = compile.matcher(baseCost);
        if (!matcher.matches()){
            compile = Pattern.compile(str2);
            matcher = compile.matcher(baseCost);
            if (!matcher.matches()){
                compile = Pattern.compile(str3);
                matcher = compile.matcher(baseCost);
                if (!matcher.matches()){
                    compile = Pattern.compile(str4);
                    matcher = compile.matcher(baseCost);
                    if (!matcher.matches()){
                        compile = Pattern.compile(str4);
                        matcher = compile.matcher(baseCost);
                        return 0;
                    }
                }
            }
        }
        return 1;
    }

    @RequestMapping("checkDescr")
    public int checkDescr(String checkDescr){
        String str="[0-9a-zA-Z_\\u4e00-\\u9fa5]{1,100}";
        Pattern compile = Pattern.compile(str);
        Matcher matcher = compile.matcher(checkDescr);
        if (matcher.matches()){
            return 1;
        }
        return 0;
    }

    @RequestMapping("doInsert")
    public int doInsert(@RequestBody Cost fee){
        Date date = new Date();
        fee.setCostCreattime(date);
        fee.setCostStarttime(date);
        return costService.insertSelective(fee);
    }
}
